package com.student.dao;

import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.student.bean.Student;

/*
 * Dao封装了数据操作方法
 */

public class Dao {
	
	private Op op = null;
	
//	public static void main(String[] args) {
//		
//		Dao dao = new Dao();
//		System.out.println(dao.getOneStudent(1).getName());
//		
//	}
	
	public Dao() {
		
		// 1. 声明配置文件目录
		String resource = "conf.xml";
		
		// 2. 加载配置文件
		InputStream in = Dao.class.getClassLoader().getResourceAsStream(resource);
		
		// 3. 创建SqlSessionFactory实例
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
		
		// 4. 获取SqlSession
		SqlSession sqlSession = sessionFactory.openSession(true);
		
		// 5. 获取数据操作类
		this.op = sqlSession.getMapper(Op.class);
		
	}
	
	/*
	 * 完成数据库操作
	 */
	
	public List<Student> getStudent(String keyword) {
		
		List<Student> stuList = op.getStudent(keyword);
		return stuList;
		
	}
	
	public Student getStudentById(int id) {
		
		Student stu = op.getStudentById(id);
		return stu;
	}
	
	public Student getStudentByNo(int no) {
		
		Student stu = op.checkStudent(no);
		return stu;
	}
	
	public boolean checkStudent(int no) {
		
		Student stu = op.checkStudent(no);
		if ( stu == null ) {
			return false;
		}
		else {
			return true;
		}
	}
	
	public void deleteStudent(int id) {
		
		op.deleteStudent(id);
		
	}
	
	public void addStudent(Student stu) {
		
		op.addStudent(stu);
		
	}
	
	public void updateStudent(Student stu) {
		
		op.updateStudent(stu);
		
	}
	
}
